package uutalk.netcloud.talk.app.ui.fragment;

import android.graphics.Color;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.text.SimpleDateFormat;
import java.util.Date;

import butterknife.BindView;
import uutalk.netcloud.talk.R;
import uutalk.netcloud.talk.app.ui.adapter.GroupHistoryAdapter;
import uutalk.netcloud.talk.base.android.BaseFragment;
import uutalk.netcloud.talk.base.mqtt.MQTTManager;
import uutalk.netcloud.talk.base.mqtt.PublishFactory;
import uutalk.netcloud.talk.base.mqtt.event.MqttMessageEvent;
import uutalk.netcloud.talk.base.mqtt.model.Publication;
import uutalk.netcloud.talk.base.util.MediaManager;
import uutalk.netcloud.talk.base.widget.RecyclerViewDivider;
import uutalk.netcloud.talk.model.BussinessBeanFactory;
import uutalk.netcloud.talk.model.bean.CMDConstant;
import uutalk.netcloud.talk.model.bean.GroupHistoryInfo;
import uutalk.netcloud.talk.model.bean.GroupHistoryListRspBean;

/**
 * Created by Administrator on 2018/1/17.
 */

public class GroupHistoryFragment extends BaseFragment {

    @BindView(R.id.recyclerview)
    XRecyclerView recyclerView;

    String groupID;
    int groupType;
    GroupHistoryAdapter groupHistoryAdapter = null;
    boolean isFresh, isLoadMore;

    public static GroupHistoryFragment newInstance(String groupID, int groupType) {
        GroupHistoryFragment fragment = new GroupHistoryFragment();
        fragment.groupID = groupID;
        fragment.groupType = groupType;
        return fragment;
    }

    @Override
    public boolean supportActionbar() {
        return true;
    }

    @Override
    public boolean isAutoBindEvent() {
        return true;
    }

    @Override
    public void onResume() {
        super.onResume();
        MediaManager.resume();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if(groupHistoryAdapter != null){
            groupHistoryAdapter.gc();
        }
        MediaManager.release();
    }

    @Override
    protected int setLayoutResId() {
        return R.layout.fragment_group_history;
    }

    @Override
    protected void loadData() {

    }

    @Override
    protected void init() {
        if(groupType == 1){
            getTitleTV().setText("历史记录");
        } else {
            getTitleTV().setText("群聊历史记录");
        }

        recyclerView.setLayoutManager(new LinearLayoutManager(mActivity, LinearLayoutManager.VERTICAL, false));
        recyclerView.addItemDecoration(new RecyclerViewDivider(mActivity, LinearLayoutManager.VERTICAL, 1, Color.WHITE));
        recyclerView.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
        if (MQTTManager.getInstance().isConnected()) {
            progressHudUtil.showProgressHud();
            SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date=new Date(System.currentTimeMillis());
            Log.e(TAG, "time>>"+ft.format(date)+"  ");
            Publication publication = PublishFactory.createPublication(BussinessBeanFactory.createDownloadTalkHistory(groupID, ft.format(date) ));
            MQTTManager.getInstance().publish(publication);
        }

        recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date date=new Date(System.currentTimeMillis());
                Publication publication = PublishFactory.createPublication(BussinessBeanFactory.createDownloadTalkHistory(groupID, ft.format(date)));
                MQTTManager.getInstance().publish(publication);
                isFresh = true;
            }

            @Override
            public void onLoadMore() {
                GroupHistoryInfo groupHistoryInfo = groupHistoryAdapter.getLastGroupHistoryInfo();
                Log.e(TAG,"groupHistoryInfo.getTime()"+groupHistoryInfo.getTime());
                Publication publication = PublishFactory.createPublication(BussinessBeanFactory.createDownloadTalkHistory(groupID, groupHistoryInfo.getTime()));
                MQTTManager.getInstance().publish(publication);
                isLoadMore = true;
            }
        });
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMqttMessageEvent(MqttMessageEvent event) {
        if (CMDConstant.DOWNLOAD_TALK_HISTORY_RESP.equals(event.getMessage().getCmd())) {
            progressHudUtil.dismissProgressHud();
            //聊天记录下载
            GroupHistoryListRspBean groupHistoryListRspBean = JSON.parseObject(event.getMessage().toString(), GroupHistoryListRspBean.class);
            if(groupHistoryAdapter == null){
                Log.e("GroupHistoryFragment","数据="+groupHistoryListRspBean.getTalk_list().toString()+"网络字段="+event.getMessage().toString());
                groupHistoryAdapter = new GroupHistoryAdapter(mActivity, groupHistoryListRspBean.getTalk_list());
                recyclerView.setAdapter(groupHistoryAdapter);
            } else {
                if(isFresh){
                    groupHistoryAdapter.deleteAll();
                    groupHistoryAdapter.addItems(groupHistoryListRspBean.getTalk_list());
                    recyclerView.refreshComplete();
                }

                if(isLoadMore){
                    groupHistoryAdapter.addItems(groupHistoryListRspBean.getTalk_list());
                    recyclerView.loadMoreComplete();
                }
            }
            isFresh = false;
            isLoadMore = false;

        }
    }


}
